# encoding: utf-8

module Capevaluation
  module Service
    class Submits < CaiQiu::Core::ServiceBase
      class << self
        # 获取数据库连接
        def get_conn
          unless defined? @conn
            @conn = CaiQiu::Core::ServiceConn.DB_CAPEVALUATION
          end
          @conn
        end



        def is_exists(params)
          info = Capevaluation::Dao::Submits.where(
              :type=>params['type'],
              :player=>params['player'],
              :match_id=>params['match_id'],
              :play_code=>params['play_code'],
              :play_odds=>params['play_odds']
          ).first
          info.nil?? false : true
        end

        def add_prediction(params)
          info = Capevaluation::Dao::Submits.new
          info[:type]  = params['type']
          info[:player] = params['player']
          info[:match_id] = params['match_id']
          info[:play_code] = params['play_code']
          info[:play_odds] = params['play_odds']
          info[:prediction] = params['prediction']
          info[:create_time] = CaiQiu::Support::CaiQiuTime.get_current_time
          info.save
        end

        def update_prediction(params)
          Capevaluation::Dao::Submits.where(
              :type=>params['type'],
              :player=>params['player'],
              :match_id=>params['match_id'],
              :play_code=>params['play_code'],
              :play_odds=>params['play_odds']
          ).update(:prediction=>params['prediction'])
        end

        def get_records_by_params(params)
          page_count = 0
          current_page = params[:page].to_i
          count = params[:count].to_i
          limit_begin = count*current_page
          #limit_end = count*(current_page+1)-1
          sql = "SELECT * FROM submits WHERE type = '#{params[:type]}' AND player = '#{params[:player]}' LIMIT #{limit_begin} ,#{count}"
          rows = self.get_conn[sql]
          res_list = []
          rows.each do |item|
            res_list.push item.to_hash
          end

          sql_page = "SELECT * FROM submits WHERE type = '#{params[:type]}' AND player = '#{params[:player]}'"
          rows_page = self.get_conn[sql_page]
          rows_page.each do |item|
            page_count += 1
          end
          total_page = page_count/count
          [res_list,total_page]
        end

        def get_wait_replay_list
          match_id_list = []
          #prediction_results = []
          sql = "SELECT distinct match_id,play_code,prediction,player,type FROM submits where matching='9';"
          rows = self.get_conn[sql]
          ret_list = []
          rows.each do |row|
            ret_list.push(row.to_hash)
            match_id_list.push(row[:match_id])
          end
          #puts ret_list
          [ret_list,match_id_list.uniq]
        end

        def update_result(match_id,play_code,pred_result,result_matching)
          sql = "update submits set matching=#{result_matching}
                 where match_id='#{match_id}' and play_code = '#{play_code}'
                 and prediction='#{pred_result}'"
          self.get_conn[sql].update
        end



        # def add_example(value, status)
        #   example          = Capevaluation::Dao::Example.new
        #   example[:value]  = value
        #   example[:status] = status
        #   example.save
        # end
        #
        # def get_example_by_value(value)
        #   example_list = []
        #   rows         = Capevaluation::Dao::Example.select(:id, :value, :status)
        #                      .where(:value => value)
        #   rows.each do |row|
        #     example_list.push row.to_hash
        #   end
        #   example_list
        # end
        #
        # def get_example_by_id(id)
        #   example_list = []
        #   rows         = Capevaluation::Dao::Example.select(:id, :value, :status)
        #                      .where(:id => id)
        #   rows.each do |row|
        #     example_list.push row.to_hash
        #   end
        #   example_list
        # end
        #
        # def list_examples
        #   example_list = []
        #   rows         = Capevaluation::Dao::Example.select(:id, :value, :status)
        #   rows.each do |row|
        #     example_list.push row.to_hash
        #   end
        #   example_list
        # end
        #
        # def mod_example(example_hash)
        #   example_model = Capevaluation::Dao::Example.select(:id, :value, :status)
        #                       .where(:id => example_hash[:id]).first
        #   example_hash.each do |k, v|
        #     if example_model.columns.include? k
        #       if v != example_model[v]
        #         example_model[k] = v
        #       end
        #     end
        #   end
        #   example_model.save_changes
        # end
        #
        # def mod_examples(example_list)
        #   example_list.each do |example|
        #     mod_example(example)
        #   end
        # end
        #
        # def mod_value_by_status(example_hash)
        #   # 开启事务
        #   example_model = Capevaluation::Dao::Example.select(:id, :value, :status)
        #                       .where(:id => example_hash[:id]).first
        #   example_model.lock!
        #   if example_model[:status] == 0
        #     example_model[:value] = 'lock'
        #     example_model.save_changes
        #   end
        # end

      end
    end
  end
end

